//
//  MLZShowViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/7.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZShowViewController.h"
#import "MLZShowTableViewCell.h"
#import "MLZShowImageTableViewCell.h"
#import "zJRequestManager.h"
#import "MLZShowModel.h"
#import "DCPicScrollView.h"
#import "MJRefresh.h"
#import "NetWorkState.h"
@interface MLZShowViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *showTableView;
@property (nonatomic, strong) NSMutableArray *arraList;
@property (nonatomic, strong) NSMutableArray *arrayPicture;
@end

@implementation MLZShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //判断是否有网络
    NetWorkState *state = [NetWorkState shareInstance];
    [state reachability];


    [self datashowPassing];

   [self datashowPassing];

    [self addtableView];
    

}
- (void)datashowPassing{
    [zJRequestManager requestWithUrl:@"http://www.qubaobei.com/ios/api/cms_list.php?last_dated=1452664800&last_id=5131&user_id=7685787&version=813" parmDic:nil method:GET succeedWithData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *dataArray = dic[@"data"];
        _arraList = [NSMutableArray array ];
        for (NSDictionary *onedic in dataArray) {
            MLZShowModel *model = [[MLZShowModel alloc]init];
            [model setValuesForKeysWithDictionary:onedic];
            [_arraList addObject:model];
        }
        [self.showTableView reloadData];
    
    } failedWithError:^(NSError *connectionError) {
        NSLog(@"%@",connectionError);
    }];
    
  
    
    
}
-(void)addtableView{
    self.showTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KW, KH-64) style:(UITableViewStylePlain)];
    _showTableView.delegate = self;
    _showTableView.dataSource = self;
    
    
    BmobQuery *que = [BmobQuery queryWithClassName:@"Picture"];
    [que calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        _arrayPicture = [NSMutableArray array];
        NSLog(@"%@",array);
        for (NSDictionary *onedic in array) {
            NSString *url = [NSString stringWithFormat:@"http://file.bmob.cn/%@",onedic[@"picture"][@"url"]];
            NSLog(@"%@",url);
            [_arrayPicture  addObject:url];
            
            
          
        }
        
        DCPicScrollView *dcp = [DCPicScrollView  picScrollViewWithFrame:CGRectMake(0, 0, KW, 150) WithImageUrls:_arrayPicture];
        dcp.AutoScrollDelay = 2.0f;
        _showTableView.tableHeaderView= dcp;

        
        
    }];

    [self.showTableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    [self.showTableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerAction)];

   [self.view addSubview:_showTableView];


}
- (void)headerAction
{
    [self datashowPassing];
     [_showTableView.header endRefreshing];
}
- (void)footerAction
{
    MLZShowModel *model = _arraList[_arraList.count - 1];
    NSString *str = [NSString stringWithFormat:@"http://www.qubaobei.com/ios/api/cms_list.php?last_dated=%ld&last_id=%@&user_id=7685787&version=813",(long)model.dated,model.dated_id];
    [zJRequestManager requestWithUrl:str parmDic:nil method:GET succeedWithData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *dataArray = dic[@"data"];
        _arraList = [NSMutableArray array ];
        for (NSDictionary *onedic in dataArray) {
            MLZShowModel *model = [[MLZShowModel alloc]init];
            [model setValuesForKeysWithDictionary:onedic];
            [_arraList addObject:model];
        }
        [self.showTableView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        NSLog(@"%@",connectionError);
    }];

     [_showTableView.footer endRefreshing];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return _arraList.count;
    


}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


static NSString *identifier = @"ZJCell";
    static NSString *identifier2= @"ZJC";
    MLZShowModel *model = _arraList[indexPath.row];
    
    if(model.pics.count==3){
    MLZShowImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
    if (!cell) {
        cell= [[MLZShowImageTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier2];
        
    }
        cell.model =model;
        
    return cell;
        
    }else{
    
        MLZShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell= [[MLZShowTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            
    }
        
        cell.model= model;
        
    return cell;
        
    }
    
    
    
    
 





}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    MLZShowModel *model = _arraList[indexPath.row];
    
    if(model.pics.count==3){
               return KW / 2;
    }else{
        
        
         return KW / 3 - 10;
    }







}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MLZShowModel *model = _arraList[indexPath.row];
  
    if([_delegate respondsToSelector:@selector(jump:)]){
        
        
        [_delegate jump:model.url];
        
    }





}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
